﻿#region using statements

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using NGen;
using NGen.Managers.GUI;

using Nuclex.UserInterface;
using Nuclex.UserInterface.Controls;
using Nuclex.UserInterface.Controls.Desktop;

using TowerDefense.Misc;
using TowerDefense.Objects.Towers;

#endregion

namespace TowerDefense.GUILayouts
{
	/// <summary>
	/// 	The gui used for the play state
	/// </summary>
	public class GUIElementPlayStateLayout : GUIElement
	{
		#region local fields

		//objects which make up this gui
		private readonly ButtonControl m_addFireButton = new ButtonControl();

		private readonly ButtonControl m_addIceButton = new ButtonControl();

		private readonly ButtonControl m_addWindButton = new ButtonControl();

		private readonly ButtonControl m_selectedBackground = new ButtonControl();

		private readonly ButtonControl m_selectedSellButton = new ButtonControl();

		private readonly LabelControl m_selectedTextLabel = new LabelControl();

		private readonly ButtonControl m_selectedUpgradeButton = new ButtonControl();

		private readonly ButtonControl m_startButton = new ButtonControl();

		private readonly ButtonControl m_textBackground = new ButtonControl();

		private readonly LabelControl m_textLabel = new LabelControl();

		private readonly LabelControl m_textValueLabel = new LabelControl();

		private Texture2D m_fireButtonTexture;

		private Texture2D m_iceButtonTexture;

		/// <summary>
		/// 	the tower which is currently selected (if any are selected)
		/// </summary>
		private GameObjectTowerBase m_selectedTower;

		private Texture2D m_windButtonTexture;

		#endregion

		#region local properties

		/// <summary>
		/// 	Get or set the selected tower
		/// </summary>
		public GameObjectTowerBase SelectedTower {
			get { return m_selectedTower; }
			set {
				m_selectedTower = value;
				//update the displayed info  for the selected tower
				_SetSelectedTowerLabel();
			}
		}

		public string SetNextLevelTimer {
			set { m_startButton.Text = "Start Level - " + value; }
		}

		#endregion

		#region local methods

		/// <summary>
		/// 	Update the information about the currently selected tower
		/// </summary>
		private void _SetSelectedTowerLabel() {
			m_selectedBackground.Children.Clear();
			m_selectedTextLabel.Text = "";

			if(m_selectedTower == null) {
				return;
			}
			if(m_selectedTower.GetType() ==
			   typeof(GameObjectTowerFire)) {
				m_selectedTextLabel.Text = "Fire Tower\n  -Bonus damage\n    over time\n";
			} else if(m_selectedTower.GetType() ==
			          typeof(GameObjectTowerIce)) {
				m_selectedTextLabel.Text = "Ice Tower\n  -Slows Enemies\n\n";
			} else if(m_selectedTower.GetType() ==
			          typeof(GameObjectTowerWind)) {
				m_selectedTextLabel.Text = "Wind Tower\n  -Long Range\n  -Faster Attacks\n";
			}

			m_selectedTextLabel.Text += "Tower Level: " + m_selectedTower.TowerLevel + "\n\nAttack Damage: " + m_selectedTower.AttackDamage + "\n\nAttack Range: " + Math.Round(m_selectedTower.AttackRange, 2) + "\n\nAttack Delay: " + Math.Round(m_selectedTower.AttackDelay, 2);
			m_selectedBackground.Children.Add(m_selectedTextLabel);

			if(m_selectedTower.Id <= 0) {
				return;
			}

			//if the tower has been placed already ( id gets assigned after placement)
			m_selectedUpgradeButton.Text = "(U)pgrade: $" + m_selectedTower.CostToUpgrade;
			m_selectedSellButton.Text = "(S)ell: $" + m_selectedTower.SellPrice;
			m_selectedBackground.Children.Add(m_selectedSellButton);
			if(m_selectedTower.TowerLevel < 5) {
				m_selectedBackground.Children.Add(m_selectedUpgradeButton);
			}

			//activate/deactivate the upgrade button if you have enough gold
			m_selectedUpgradeButton.Enabled = (GameEngine.GetService<GameStats>().Gold >= m_selectedTower.CostToUpgrade);
		}

		#endregion

		#region GUIElement implementation

		/// <summary>
		/// 	_Initialize this gui
		/// </summary>
		protected override void _Initialize() {
			//load the images for the new tower buttons
			m_fireButtonTexture = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\Towers\\fire_1");
			m_iceButtonTexture = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\Towers\\ice_1");
			m_windButtonTexture = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\Towers\\wind_1");

			//create the background for the game stats display
			m_textBackground.Bounds = new UniRectangle(615, 12, 160, 115);
			m_textBackground.Enabled = false;

			//create the text label for the game stats display
			m_textLabel.Bounds = new UniRectangle(15, 18, 0, 0);
			m_textLabel.Text = "Gold\n\nLives\n\nLevel\n\n";
			m_textBackground.Children.Add(m_textLabel);

			//label which displays the numbers in the game stats display
			m_textValueLabel.Bounds = new UniRectangle(60, 18, 0, 0);
			m_textBackground.Children.Add(m_textValueLabel);

			m_startButton.Bounds = new UniRectangle(615, 229, 160, 40);
			m_startButton.Text = "Start Level";
			m_startButton.Pressed += delegate { ThrowGuiEvent("startPressed"); };

			m_addFireButton.Bounds = new UniRectangle(615, 160, m_fireButtonTexture.Width + 4, m_fireButtonTexture.Height + 4);
			m_addFireButton.Pressed += delegate { ThrowGuiEvent("firePressed"); };

			m_addIceButton.Bounds = new UniRectangle(677, 160, m_iceButtonTexture.Width + 4, m_iceButtonTexture.Height + 4);
			m_addIceButton.Pressed += delegate { ThrowGuiEvent("icePressed"); };

			m_addWindButton.Bounds = new UniRectangle(739, 160, m_windButtonTexture.Width + 4, m_windButtonTexture.Height + 4);
			m_addWindButton.Pressed += delegate { ThrowGuiEvent("windPressed"); };

			_Children.Add(m_textBackground);
			_Children.Add(m_startButton);
			_Children.Add(m_addFireButton);
			_Children.Add(m_addIceButton);
			_Children.Add(m_addWindButton);

			//background for the tower stats display
			m_selectedBackground.Bounds = new UniRectangle(615, 302, 160, 286);
			m_selectedBackground.Enabled = false;

			m_selectedUpgradeButton.Bounds = new UniRectangle(5, 196, 150, 40);
			m_selectedUpgradeButton.Pressed += delegate { ThrowGuiEvent("upgradePressed"); };

			m_selectedSellButton.Bounds = new UniRectangle(5, 241, 150, 40);
			m_selectedSellButton.Pressed += delegate { ThrowGuiEvent("sellPressed"); };
			m_selectedTextLabel.Bounds = new UniRectangle(5, 10, 0, 0);
			_Children.Add(m_selectedBackground);
		}

		/// <summary>
		/// 	Update this gui
		/// </summary>
		/// <param name = "p_gameTime">snapshot of timing values</param>
		protected override void Update(GameTime p_gameTime) {
			//update the numbers for the game stats display
			m_textValueLabel.Text = GameEngine.GetService<GameStats>().Gold + "\n\n" + GameEngine.GetService<GameStats>().Lives + "\n\n" + GameEngine.GetService<GameStats>().Level + "\n\n";
			_SetSelectedTowerLabel();
		}

		/// <summary>
		/// 	Draw this gui
		/// </summary>
		/// <param name = "p_gameTime">snapshot of timing values</param>
		protected override void _Draw(GameTime p_gameTime) {
			//draw the fire tower image
			GameEngine.SpriteBatch.Draw(m_fireButtonTexture, new Vector2(m_addFireButton.GetAbsoluteBounds().X + 2, m_addFireButton.GetAbsoluteBounds().Y + 2), null, Color.White);

			//draw the ice tower image
			GameEngine.SpriteBatch.Draw(m_iceButtonTexture, new Vector2(m_addIceButton.GetAbsoluteBounds().X + 2, m_addIceButton.GetAbsoluteBounds().Y + 2), null, Color.White);

			//draw the wind tower image
			GameEngine.SpriteBatch.Draw(m_windButtonTexture, new Vector2(m_addWindButton.GetAbsoluteBounds().X + 2, m_addWindButton.GetAbsoluteBounds().Y + 2), null, Color.White);
		}

		/// <summary>
		/// 	Cleanup this gui
		/// </summary>
		protected override void _Cleanup() {
			//no cleanup necessary outside of the normal memory deallocation
			//C# takes care of this via garbage collection
		}

		#endregion
	}
}